﻿/////////////////////////////////////////////////////////////////////////////////
//
// InstantCube
// - Automatically generates Analysis Services cubes
// (C) Copyright Johan Åhlén 2009
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
// See the GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
//
// More info on InstantCube website: http://instantcube.codeplex.com
//
/////////////////////////////////////////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace AdvantIQ.InstantCube.Model
{
    public static class ExtensionMethods
    {
        public static string SafeValue(this XAttribute attribute)
        {
            return attribute != null ? attribute.Value : "";
        }

        public static string SafeValue(this XAttribute attribute, string defaultValue)
        {
            return attribute != null ? attribute.Value : defaultValue;
        }

        //public static string RemoveQuotes(this string str)
        //{
        //    if (str == null)
        //        return null;
        //    else if (str.StartsWith("\"") && str.EndsWith("\""))
        //        return str.Substring(1, str.Length - 2);
        //    else
        //        return str;
        //}

        //public static string Default(this string str, string defaultValue)
        //{
        //    if (str == null)
        //        return defaultValue;
        //    else
        //        return str;
        //}

        //public static IEnumerable<Node> GetSubNodes(this Node node, string brand)
        //{
        //    if (node == null)
        //        return new Node[0];

        //    return from n in node.ViewAllNodes()
        //           where string.Compare(n.Brand.Text, brand, true) == 0
        //           select n;
        //}

        //public static Node GetSubNode(this Node node, string brand)
        //{
        //    if (node == null)
        //        return null;

        //    var nodes = GetSubNodes(node, brand);

        //    return nodes.DefaultIfEmpty(null).Single();
        //}

        //public static string[] GetNodeStrings(this Node node, Func<string, string> func, int count)
        //{
        //    if (func == null && count == 0)
        //        return node.ViewAllNodes().Select(n => (string)n.AtomicValue).ToArray();
        //    else if (func == null && count > 0)
        //        return node.ViewAllNodes().Take(count).Select(n => (string)n.AtomicValue).ToArray();
        //    else if (func != null && count == 0)
        //        return node.ViewAllNodes().Select(n => func((string)n.AtomicValue)).ToArray();
        //    else
        //        return node.ViewAllNodes().Take(count).Select(n => func((string)n.AtomicValue)).ToArray();
        //}

        //public static string[] GetUnquotedNodeStrings(this Node node)
        //{
        //    var nodes = from n in node.ViewAllNodes()
        //                select ((string)n.AtomicValue).RemoveQuotes();

        //    return nodes.ToArray();
        //}

        //public static string Attribute(this Node node, string label)
        //{
        //    if (node.Edges.ContainsLabel(label))
        //    {
        //        return ((string)node.Edges[label].AtomicValue).RemoveQuotes();
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}

        //public static string IsNull(this string srcString, string defaultValue)
        //{
        //    return srcString != null ? srcString : defaultValue;
        //}
    }
}
